package com.ubaer.zsycz.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lsw.Base.BaseHolder;
import com.lsw.Base.BaseSimpleAdapter;
import com.lsw.util.LogUtils;
import com.ubaer.zsycz.R;
import com.ubaer.zsycz.model.OrderModel;

/**
 * Created by Administrator on 2018/9/25 0025.
 */

public class UntreatedAdapter extends BaseSimpleAdapter<OrderModel> {
    public UntreatedAdapter(Context context) {
        super(context);
    }

    @Override
    protected BaseHolder<OrderModel> getHolder() {
        return new BaseHolder<OrderModel>() {
            View view;
            LinearLayout ll_seclect;
            CheckBox checkBox;
            TextView tV_chuli;
            TextView tv_order_no;
            TextView tv_car_number;
            TextView tv_price;

            @Override
            public void bindViews(View parentView) {
                view = parentView.findViewById(R.id.view);
                checkBox = parentView.findViewById(R.id.checkBox);
                tV_chuli = parentView.findViewById(R.id.tV_chuli);
                tv_order_no = parentView.findViewById(R.id.tv_order_no);
                tv_car_number = parentView.findViewById(R.id.tv_car_number);
                tv_price = parentView.findViewById(R.id.tv_price);
                ll_seclect = parentView.findViewById(R.id.ll_seclect);
            }

            @Override
            public void bindData(OrderModel item, int position) {
                if (position == 0) {
                    view.setVisibility(View.VISIBLE);
                } else {
                    view.setVisibility(View.GONE);
                }
                tv_order_no.setText(item.getOrder_no());
                tv_car_number.setText(item.getCard_num());
                tv_price.setText(item.getMoney());

                checkBox.setChecked(item.isIscheck());
                ll_seclect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        checkBox.setChecked(!checkBox.isChecked());
                        item.setIscheck(checkBox.isChecked());
                        notifyDataSetChanged();
                        isall();
                    }
                });
                checkBox.setOnClickListener(v -> {
                    item.setIscheck(checkBox.isChecked());
                    notifyDataSetChanged();
                    isall();
                });
                tV_chuli.setOnClickListener(v -> {
                    if (adapterRefresh != null)
                        adapterRefresh.onRefreshAdapter(position, 3);
                });

            }
        };
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.item_untreated;
    }

    int ischeck;

    /**
     * 判断是否全选
     *
     * @return
     */
    public void isall() {
        ischeck = 0;
        boolean all = true;
        for (int i = 0; i < list.size(); i++) {
            if (!list.get(i).isIscheck()) {
                all = false;
            } else {
                ischeck++;
            }
        }
        if (all && list.size() > 0) {
            if (adapterRefresh != null)
                adapterRefresh.onRefreshAdapter(ischeck, 1);
        } else {
            if (adapterRefresh != null)
                adapterRefresh.onRefreshAdapter(ischeck, 2);
        }
    }

    /**
     * 是否全选中
     *
     * @param isall
     */
    public void all(boolean isall) {
        if (isall) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setIscheck(true);
            }

        } else {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setIscheck(false);
            }
        }
        notifyDataSetChanged();

    }
}
